home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / J_AUTHOR / Mojo / data.2 / docs / microline / examples / grid5.java < prev    next >
Encoding:
Java Source  |  1996-05-27  |  14.4 KB  |  506 lines

  1. // (c) Copyright 1994-1996 Microline Software, Inc. ALL RIGHTS RESERVED
  2. //
  3. // THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE COPIED AND USED
  4. // ONLY IN ACCORDANCE WITH THE TERMS OF THAT LICENSE AND WITH THE INCLUSION
  5. // OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE AND DOCUMENTATION, AND ITS
  6. // COPYRIGHTS ARE OWNED BY MICROLINE SOFTWARE AND ARE PROTECTED BY UNITED
  7. // STATES COPYRIGHT LAWS AND INTERNATIONAL TREATY PROVISIONS.
  8. //
  9. // THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  10. // AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY MICROLINE SOFTWARE.
  11. //
  12. // THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT
  13. // WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY
  14. // PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT.  MICROLINE SOFTWARE
  15. // ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS
  16. // SOFTWARE.
  17. //
  18. // MICROLINE SOFTWARE SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR
  19. // CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. SOME
  20. // STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR
  21. // CONSEQUENTIAL DAMAGES, SO THE ABOVE LIMITATIONS MIGHT NOT APPLY TO
  22. // YOU.
  23. //
  24. // MICROLINE SOFTWARE SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE
  25. // ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES
  26. // RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE
  27. // TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY
  28. // MICROLINE SOFTWARE.
  29. //
  30. // U.S. GOVERNMENT RESTRICTED RIGHTS
  31. // This Software and documentation are provided with RESTRICTED RIGHTS.
  32. // Use, duplication or disclosure by the Government is subject to
  33. // restrictions as set forth in subparagraph (c)(1) of the Rights in
  34. // Technical Data and Computer Software Clause at DFARS 252.227-7013 or
  35. // subparagraphs (c)(1)(ii) and (2) of Commercial Computer Software -
  36. // Restricted Rights at 48 CFR 52.227-19, as applicable, supplier is
  37. // Microline Software, 41 Sutter St Suite 1374, San Francisco, CA 94104.
  38.  
  39. import java.awt.*;
  40. import java.util.*;
  41. import java.mct.*;
  42. import java.applet.Applet;
  43.  
  44. // ---- mini-database prototype
  45.  
  46. // database column
  47. class DbColumn
  48. {
  49. public String name;
  50. public int width;
  51. public int alignment;
  52. public boolean editable;
  53.  
  54. public DbColumn(String _name, int _width, int _alignment, boolean _editable)
  55.     {
  56.     name = _name;
  57.     width = _width;
  58.     alignment = _alignment;
  59.     editable = _editable;
  60.     }
  61. }
  62.  
  63. // database row
  64. class DbRow extends MlArrayItem
  65. {
  66. public String key;
  67. public String desc;
  68. public double price;
  69. public int qty;
  70. public String buyer;
  71.  
  72. public DbRow(String _key, String _desc, double _price, int _qty, String _buyer)
  73.     {
  74.     key = _key;
  75.     desc = _desc;
  76.     price = _price;
  77.     qty = _qty;
  78.     buyer = _buyer;
  79.     }
  80. }
  81.  
  82. // database table
  83. class DbTable extends MlArray
  84. {
  85. Vector columns;
  86. String sortColumn;
  87.  
  88. public DbTable()
  89.     {
  90.     columns = new Vector();
  91.  
  92.     addColumn("Description", 16, MlGrid.ALIGNMENT_LEFT,  true );
  93.     addColumn("Price",       9,  MlGrid.ALIGNMENT_RIGHT, true );
  94.     addColumn("Qty",         5,  MlGrid.ALIGNMENT_RIGHT, true );
  95.     addColumn("Unit Prc",    9,  MlGrid.ALIGNMENT_RIGHT, false);
  96.     addColumn("Buyer",       15, MlGrid.ALIGNMENT_LEFT,  true );
  97.  
  98.     addRow("key01", "Staples",        1.32, 100, "Tim Pick"     );
  99.     addRow("key02", "Notebooks",      1.11,   4, "Mary Miner"   );
  100.     addRow("key03", "3-Ring Binders", 2.59,   2, "Mary Miner"   );
  101.     addRow("key04", "Pads",           1.23,   3, "Tim Pick"     );
  102.     addRow("key05", "Scissors",       4.41,   1, "Mary Miner"   );
  103.     addRow("key06", "Pens",            .29,   4, "Mary Miner"   );
  104.     addRow("key07", "Pencils",         .10,   5, "Tim Pick"     );
  105.     addRow("key08", "Markers",         .95,   3, "Mary Miner"   );
  106.     addRow("key09", "Fax Paper",      3.89, 100, "Bob Coal"     );
  107.     addRow("key10", "3.5\" Disks",   15.23,  30, "Tim Pick"     );
  108.     addRow("key11", "8mm Tape",      32.22,   2, "Bob Coal"     );
  109.     addRow("key12", "Toner",         35.69,   1, "Tim Pick"     );
  110.     addRow("key13", "Paper Cups",     4.25,   3, "Bob Coal"     );
  111.     addRow("key14", "Paper Clips",    2.09,   3, "Tim Pick"     );
  112.     addRow("key15", "Small Post Its", 2.20,   4, "Ezra Epstein" );
  113.     addRow("key16", "Pink Hi-Liters", 0.85,  30, "Olivia Semak" );
  114.     addRow("key17", "Letter Folders", 4.25,  25, "Susan Redford");
  115.     addRow("key18", "Legal Folders",  2.39,  10, "Jane Maple"   );
  116.     addRow("key19", "Erasers",        8.00, 100, "Anna Nguyen"  );
  117.     addRow("key20", "White-Out",     12.00,  12, "Joanna Keefe" );
  118.     }
  119.  
  120. public void addColumn(String name, int width, int alignment, boolean editable)
  121.     {
  122.     columns.addElement(new DbColumn(name, width, alignment, editable));
  123.     }
  124.  
  125. public void addRow(String key, String desc, double price,
  126.     int qty, String buyer)
  127.     {
  128.     addItem(new DbRow(key, desc, price, qty, buyer));
  129.     }
  130.  
  131. public DbColumn getColumn(int i)
  132.     {
  133.     return (DbColumn)columns.elementAt(i);
  134.     }
  135.  
  136. public int getColumnCount()
  137.     {
  138.     return columns.size();
  139.     }
  140.  
  141. public DbRow findRow(String key)
  142.     {
  143.     DbRow row;
  144.     int i;
  145.  
  146.     for (i = 0; i < getCount(); i++)
  147.         {
  148.         row = (DbRow)get(i);
  149.         if (key.equals(row.key))
  150.             return row;
  151.         }
  152.     return null;
  153.     }
  154.  
  155. public int compare(MlArrayItem item1, MlArrayItem item2)
  156.     {
  157.     DbRow row1, row2;
  158.     double u1, u2;
  159.  
  160.     row1 = (DbRow)item1;
  161.     row2 = (DbRow)item2;
  162.     if (sortColumn.equals("Description"))
  163.         return row1.desc.compareTo(row2.desc);
  164.     if (sortColumn.equals("Price"))
  165.         {
  166.         u1 = row1.price - row2.price;
  167.         if (u1 < 0)
  168.             return -1;
  169.         else if (u1 == 0)
  170.             return 0;
  171.         return 1;
  172.         }
  173.     if (sortColumn.equals("Qty"))
  174.         return row1.qty - row2.qty;
  175.     if (sortColumn.equals("Unit Prc"))
  176.         {
  177.         u1 = row1.price / row1.qty;
  178.         u2 = row2.price / row2.qty;
  179.         if (u1 < u2)
  180.             return -1;
  181.         else if (u1 == u2)
  182.             return 0;
  183.         else
  184.             return 1;
  185.         }
  186.     if (sortColumn.equals("Buyer"))
  187.         return row1.buyer.compareTo(row2.buyer);
  188.     return super.compare(item1, item2);
  189.     }
  190.  
  191. String []getRowKeysSorted(String columnName)
  192.     {
  193.     String keys[];
  194.     int i, rowCount;
  195.  
  196.     sortColumn = columnName;
  197.     sort();
  198.     rowCount = getCount();
  199.     keys = new String[rowCount];
  200.     for (i = 0; i < rowCount; i++)
  201.         keys[i] = ((DbRow)get(i)).key;
  202.     return keys;
  203.     }
  204. }
  205.  
  206. // mini-database grid prototype
  207.  
  208. class DbGrid extends MlGrid
  209. {
  210. DbTable table;
  211.  
  212. public DbGrid()
  213.     {
  214.     MlResources res;
  215.  
  216.     res = new MlResources();
  217.     res.add("horizontalSizePolicy", "SIZE_TO_FIT");
  218.     res.add("visibleRows", 10);
  219.     res.add("vsbDisplayPolicy", "DISPLAY_ALWAYS");
  220.     res.add("selectionPolicy", "SELECT_NONE");
  221.     res.add("shadowThickness", 0);
  222.     res.add("highlightRowMode", false);
  223.     res.add("headingRows", 1);
  224.     setValues(res);
  225.    }
  226.  
  227. public void loadTable(DbTable _table)
  228.     {
  229.     MlResources res;
  230.     DbColumn col;
  231.     int i, numColumns;
  232.  
  233.     table = _table;
  234.     res = new MlResources();
  235.     setValue("layoutFrozen", true);
  236.  
  237.     deleteAllColumns();
  238.     // delete all content rows, not heading row
  239.     deleteAllRows();
  240.  
  241.     // set cell defaults for heading row
  242.     res.clear();
  243.     res.add("cellDefaults", true);
  244.     res.add("cellLeftBorderType", "BORDER_LINE");
  245.     res.add("cellRightBorderType", "BORDER_LINE");
  246.     res.add("cellTopBorderType", "BORDER_LINE");
  247.     res.add("cellBottomBorderType", "BORDER_LINE");
  248.     res.add("cellBackground", "#C0C0C0");
  249.     res.add("cellLeftMargin", 1);
  250.     res.add("cellRightMargin", 1);
  251.     setValues(res);
  252.  
  253.     // add columns - this creates cells in the heading row
  254.     numColumns = table.columns.size();
  255.     addColumns(numColumns);
  256.  
  257.     for (i = 0; i < numColumns; i++)
  258.         {
  259.         col = table.getColumn(i);
  260.  
  261.         // set the width and the key (name) on columns in the grid
  262.         res.clear();
  263.         res.add("column", i);
  264.         res.add("columnUserObject", col.name);
  265.         res.add("columnWidth", col.width);
  266.         setValues(res);
  267.  
  268.         // set default cell alignment/editibility for cells in the column
  269.         res.clear();
  270.         res.add("cellDefaults", true);
  271.         res.add("column", i);
  272.         res.add("cellAlignment", col.alignment);
  273.         res.add("cellEditable", col.editable);
  274.         setValues(res);
  275.  
  276.         // set the column heading
  277.         res.clear();
  278.         res.add("rowType", "HEADING");
  279.         res.add("row", 0);
  280.         res.add("column", i);
  281.         res.add("cellAlignment", col.alignment);
  282.         res.add("cellString", col.name);
  283.         setValues(res);
  284.         }
  285.  
  286.     // set cell defaults for content rows
  287.     res.clear();
  288.     res.add("cellDefaults", true);
  289.     res.add("cellLeftBorderType", "BORDER_NONE");
  290.     res.add("cellRightBorderType", "BORDER_NONE");
  291.     res.add("cellTopBorderType", "BORDER_NONE");
  292.     res.add("cellBottomBorderType", "BORDER_NONE");
  293.     res.add("cellBackground", "#FFFFFF");
  294.     setValues(res);
  295.  
  296.     addRows(table.getCount());
  297.     setValue("layoutFrozen", false);
  298.     }
  299.  
  300. public void setRowKeysSorted(String sortColumn)
  301.     {
  302.     MlResources res;
  303.     String keys[];
  304.     int i;
  305.  
  306.     res = new MlResources();
  307.     keys = table.getRowKeysSorted(sortColumn);
  308.     // place row keys in each grid row's userObject
  309.     for (i = 0; i < keys.length; i++)
  310.         {
  311.         res.clear();
  312.         res.add("row", i);
  313.         res.add("rowUserObject", keys[i]);
  314.         setValues(res);
  315.         }
  316.     }
  317.  
  318. public boolean handleEvent(Event _event)
  319.     {
  320.     MlGridEvent event;
  321.     boolean handled;
  322.  
  323.     handled = false;
  324.     if (_event.id == MlGridEvent.SELECT_CELL)
  325.         {
  326.         event = (MlGridEvent)_event;
  327.         if (event.rowType == HEADING)
  328.             {
  329.             sortByColumn(event.columnType, event.column);
  330.             handled = true;
  331.             }
  332.         }
  333.     // for a full production version, this function should also
  334.     // handle EDIT_INSERT by retrieving the current value
  335.     // from the database and performing an setText() on
  336.     // the text child of the grid with that value.  This allows
  337.     // a user to hit insert or F2 to modify an existing cell value
  338.     else if (_event.id == MlGridEvent.EDIT_COMPLETE)
  339.         {
  340.         event = (MlGridEvent)_event;
  341.         editCell(event.row, event.column);
  342.         handled = true;
  343.         }
  344.     if (handled)
  345.         return true;
  346.     return super.handleEvent(_event);
  347.     }
  348.  
  349. public void sortByColumn(int colType, int col)
  350.     {
  351.     String columnName;
  352.  
  353.     editCancel();
  354.     columnName = (String)getColumnValue(colType, col, "columnUserObject");
  355.     setRowKeysSorted(columnName);
  356.     redrawAll();
  357.     }
  358.  
  359. public void editCell(int r, int c)
  360.     {
  361.     DbRow row;
  362.     MlResources res;
  363.     String string, rowKey, colKey;
  364.     boolean redrawRow;
  365.  
  366.     // get value entered
  367.     string = (String)getCellValue(CONTENT, r, CONTENT, c, "cellString");
  368.  
  369.     // retrieve row and column key
  370.     rowKey = (String)getRowValue(CONTENT, r, "rowUserObject");
  371.     colKey = (String)getColumnValue(CONTENT, c, "columnUserObject");
  372.  
  373.     // set new value in the database
  374.     redrawRow = false;
  375.     row = table.findRow(rowKey);
  376.     if (colKey.equals("Description"))
  377.         row.desc = string;
  378.     else if (colKey.equals("Price"))
  379.         {
  380.         try
  381.             {
  382.             row.price = Double.valueOf(string).doubleValue();
  383.             } catch (NumberFormatException e) {}
  384.         redrawRow = true;
  385.         }
  386.     else if (colKey.equals("Qty"))
  387.         {
  388.         try
  389.             {
  390.             row.qty = Integer.valueOf(string).intValue();
  391.             } catch (NumberFormatException e) {}
  392.         redrawRow = true;
  393.         }
  394.     else if (colKey.equals("Buyer"))
  395.         row.buyer = string;
  396.  
  397.     // set cellString to null since we draw the value
  398.     res = new MlResources();
  399.     res.add("row", r);
  400.     res.add("column", c);
  401.     res.add("cellString", null);
  402.     setValues(res);
  403.  
  404.     // redraw the row if we need to redisplay/recompute unit price
  405.     if (redrawRow)
  406.         redrawRow(CONTENT, r);
  407.     }
  408.  
  409. public void drawCell(MlGridCell cell, MlGridDrawInfo drawInfo)
  410.     {
  411.     Graphics g;
  412.     String rowKey, colKey;
  413.     DbRow row;
  414.     String string;
  415.     Rectangle rect;
  416.     MlGridCellValues values;
  417.     int horizMargin, vertMargin;
  418.  
  419.     super.drawCell(cell, drawInfo);
  420.     if (drawInfo.rowType != CONTENT)
  421.         return;
  422.  
  423.     // retrieve row key from userObject in row
  424.     rowKey = (String)getRowValue(CONTENT, drawInfo.row, "rowUserObject");
  425.  
  426.     // retrieve column key (name) from userObject in column
  427.     colKey = (String)getColumnValue(drawInfo.columnType, drawInfo.column,
  428.         "columnUserObject");
  429.  
  430.     // retrieve value from the database
  431.     row = table.findRow(rowKey);
  432.     string = null;
  433.     if (colKey.equals("Description"))
  434.         string = row.desc;
  435.     else if (colKey.equals("Price"))
  436.         string = "$" + formatDouble(row.price);
  437.     else if (colKey.equals("Qty"))
  438.         string = Integer.toString(row.qty);
  439.     else if (colKey.equals("Unit Prc"))
  440.         string = "$" + formatDouble(row.price / (double)row.qty);
  441.     else if (colKey.equals("Buyer"))
  442.         string = row.buyer;
  443.  
  444.     // create a draw rectangle, adjusted for cell margins
  445.     values = cell.getValues();
  446.     horizMargin = values.leftMargin + values.rightMargin;
  447.     vertMargin = values.topMargin + values.bottomMargin;
  448.     if (horizMargin >= drawInfo.cellRect.width ||
  449.         vertMargin >= drawInfo.cellRect.height)
  450.         return;
  451.     rect = new Rectangle();
  452.     rect.x = drawInfo.cellRect.x + values.leftMargin;
  453.     rect.y = drawInfo.cellRect.y + values.topMargin;
  454.     rect.width = drawInfo.cellRect.width - horizMargin;
  455.     rect.height = drawInfo.cellRect.height - vertMargin;
  456.  
  457.     // draw the string
  458.     g = drawInfo.g;
  459.     if (drawInfo.drawSelected)
  460.         g.setColor(drawInfo.selectForeground);
  461.     else
  462.         g.setColor(values.foreground);
  463.     MlUtil.drawString(g, string, values.font, values.alignment,
  464.         rect, drawInfo.clipRect);
  465.     }
  466.  
  467. String formatDouble(double d)
  468.     {
  469.     String string;
  470.     int i;
  471.  
  472.     string = Double.toString(d);
  473.     i = string.indexOf('.');
  474.     if (i < 0 || string.length() - i <= 3)
  475.         return string;
  476.     return string.substring(0, i + 2);
  477.     }
  478. }
  479.  
  480. // --- the application
  481.  
  482. public class grid5 extends Applet
  483. {
  484. DbGrid grid;
  485. DbTable table;
  486.  
  487. public void init()
  488.     {
  489.     Dimension d;
  490.  
  491.     setLayout(null);
  492.     setBackground(new Color(255, 255, 255));
  493.  
  494.     grid = new DbGrid();
  495.     table = new DbTable();
  496.  
  497.     grid.loadTable(table);
  498.     grid.setRowKeysSorted("Description");
  499.  
  500.     d = grid.preferredSize();
  501.     grid.reshape((bounds().width - d.width) / 2, (bounds().height -
  502.         d.height) / 2, d.width, d.height);
  503.     add(grid);
  504.     }
  505. }
  506.